/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medicalstorebilling.ui.tab2purchase;

import database.entity.Customers;
import database.entity.Distributors;
import database.entity.Medicines;
import database.entity.StockDetails;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import medicalstorebilling.ui.tab1order.Phase2Order;
import medicalstorebilling.ui.tab3medicine.MedicineSelector;
import medicalstorebilling.ui.tab3medicine.StockSelector;
import medicalstorebilling.ui.tab4customer.CustomerSelector;
import medicalstorebilling.ui.tab5distributor.DistributorSelector;

/**
 *
 * @author Administrator
 */
public class SelectorPopup extends javax.swing.JDialog {

    /**
     * Creates new form Selection
     */
    private Object needToSelect;
    private Object parentPanel;

    public Object getNeedToSelect() {
        return needToSelect;
    }

    public void setNeedToSelect(Object needToSelect) {
        this.needToSelect = needToSelect;
    }

    public SelectorPopup(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        GroupLayout layout = (GroupLayout) this.getContentPane().getLayout();
        DistributorSelector distributor = new DistributorSelector();
        layout.replace(jPanel1, distributor);
    }

    public SelectorPopup(java.awt.Frame parent, boolean modal, Object needToSelect, Object parentPanel) {
        super(parent, modal);
        this.needToSelect = needToSelect;
        this.parentPanel = parentPanel;
        initComponents();
        GroupLayout layout = (GroupLayout) this.getContentPane().getLayout();
        if (this.needToSelect instanceof Medicines) {
            MedicineSelector medicine = new MedicineSelector((Medicines) this.needToSelect, this);
            layout.replace(jPanel1, medicine);
            this.setTitle("Select Medicines");
        }
        if (this.needToSelect instanceof Distributors) {
            DistributorSelector distributor = new DistributorSelector((Distributors) this.needToSelect, this);
            layout.replace(jPanel1, distributor);
            this.setTitle("Select Distributor");
        }
        if (this.needToSelect instanceof Customers) {
            CustomerSelector customer = new CustomerSelector((Customers) this.needToSelect, this);
            layout.replace(jPanel1, customer);
            this.setTitle("Select Customer");
        }
        if (this.needToSelect instanceof StockDetails) {
            StockSelector stock = new StockSelector((StockDetails) this.needToSelect, this);
            layout.replace(jPanel1, stock);
            this.setTitle("Select Stock of Medicine");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Select");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(0, 500));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 474, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 406, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 474, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (this.needToSelect != null) {
            if (parentPanel instanceof Phase2Purchase) {
                Phase2Purchase pn = (Phase2Purchase) parentPanel;
                if (this.needToSelect instanceof Distributors) {
                    pn.setDistributor((Distributors) this.needToSelect);
                } else if (this.needToSelect instanceof Medicines) {
                    pn.setMed((Medicines) this.needToSelect);
                }
            } else if (parentPanel instanceof Phase2Order) {
                Phase2Order pn = (Phase2Order) parentPanel;
                if (this.needToSelect instanceof Customers) {
                    pn.setCustomer((Customers) this.needToSelect);
                } else if (this.needToSelect instanceof StockDetails) {
                    pn.setStock((StockDetails) this.needToSelect);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Nothing to select", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
